<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('题库管理')">导出</a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>
      <!-- 高级查询区域 -->
      <j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :expandedRowKeys="expandedRowKeys"
        :expandRowByClick="expandRowByClick"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange" @expand="handleExpand">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text,record">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" :preview="record.id" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click.stop="handleEdit(record)">编辑</a>

          <a-divider type="vertical" />
          <a @click.stop="handleAddItem(record)">添加题项</a>
          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>


        <a-table slot="expandedRowRender" slot-scope="text" :columns="innerColumns" :dataSource="innerData" size="middle"
                 bordered rowKey="id" :pagination="false" :loading="loading">
          <span slot="urlSlot" slot-scope="text, record">
            <a :href="record.filePath" target="_blank">{{ text }}</a>
          </span>
          <template slot="imgSlot" slot-scope="text">
            <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
            <img v-else :src="getImgView(text)" height="25px" alt=""
                 style="max-width:80px;font-size: 12px;font-style: italic;" @click="showPreImg(text)" />
          </template>
          <span slot="actionInner" slot-scope="text, record">
            <a @click="handleEditInner(record)">修改</a>
            <a-divider type="vertical" />
            <a-popconfirm title="确定删除吗?" @confirm="() => handleDeleteInner(record)">
              <a>删除</a>
            </a-popconfirm>
          </span>
        </a-table>
      </a-table>
    </div>

    <mft-topic-modal ref="modalForm" @ok="modalFormOk"></mft-topic-modal>
    <topic-item-modal ref="addItemForm" @okInner="handleModalFormOk"></topic-item-modal>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import MftTopicModal from './modules/MftTopicModal'
  import { loadCategoryData } from '@/api/api'
  import {filterMultiDictText} from '@/components/dict/JDictSelectUtil'
  import { deleteAction, getAction } from '@api/manage'
  import TopicItemModal from '@views/topicitem/modules/TopicItemModal.vue'

  export default {
    name: 'MftTopicList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      TopicItemModal,
      MftTopicModal
    },
    data () {
      return {
        description: '题库管理管理页面',
        // 子表表头
        innerColumns: [
          {
            title: '题目编号',
            align: 'center',
            dataIndex: 'topicId',
            key: 'id'
          },
          {
            title: '题项',
            align: 'center',
            dataIndex: 'code',
            customRender: (text, row, index) => {
              let c = '';
              switch (text) {
                case '1':
                  c = 'A';
                  break;
                case '2':
                  c = 'B';
                  break;
                case '3':
                  c = 'C';
                  break;
                case '4':
                  c = 'D';
                  break;
              }
              return c;
            }
          },
          {
            title: '题项内容',
            align: 'center',
            dataIndex: 'content'
          },
          {
            title: '是否正确答案',
            align: 'center',
            dataIndex: 'correct',
            customRender: (text, row, index) => {
              return text ? '是' : '否'
            }
          },
          {
            title: '操作',
            dataIndex: 'actionInner',
            align: 'center',
            scopedSlots: { customRender: 'actionInner' }
          }
        ],
        innerData: [],
        expandedRowKeys: [],
        expandRowByClick: true,
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'题目题干',
            align:"center",
            dataIndex: 'topicTitle'
          },
          {
            title:'专项类别',
            align:"center",
            dataIndex: 'specialType',
            customRender: (text) => (text ? filterMultiDictText(this.dictOptions['specialType'], text) : '')
          },
          {
            title:'题型',
            align:"center",
            dataIndex: 'topicType_dictText'
          },
          {
            title:'答案解析',
            align:"center",
            dataIndex: 'answer'
          },
          {
            title:'分数',
            align:"center",
            dataIndex: 'score'
          },
          {
            title:'是否必练题',
            align:"center",
            dataIndex: 'must_dictText'
          },
          {
            title:'状态',
            align:"center",
            dataIndex: 'status_dictText'
          },
          {
            title:'题目配图',
            align:"center",
            dataIndex: 'topicPicture',
            scopedSlots: {customRender: 'fileSlot'}
          },
          {
            title:'是否付费',
            align:"center",
            dataIndex: 'isPay_dictText'
          },
          {
            title:'对错',
            align:"center",
            dataIndex: 'isRight_dictText'
          },
          {
            title:'题目类别',
            align:"center",
            dataIndex: 'topicCategory',
            customRender: (text) => (text ? filterMultiDictText(this.dictOptions['topicCategory'], text) : '')
          },
          {
            title:'备注',
            align:"center",
            dataIndex: 'remark'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            width:147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/mfttopic/mftTopic/list",
          delete: "/mfttopic/mftTopic/delete",
          deleteBatch: "/mfttopic/mftTopic/deleteBatch",
          exportXlsUrl: "/mfttopic/mftTopic/exportXls",
          importExcelUrl: "mfttopic/mftTopic/importExcel",
          itemList: "/topicitem/topicItem/list",
          itemDelete: "/topicitem/topicItem/delete",
        },
        dictOptions:{},
        superFieldList:[],
      }
    },
    created() {
    this.getSuperFieldList();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      handleAddItem(record){
        console.log("添加题项")
        if (record.topicType_dictText == '判断题') {
          this.$message.warn("判断题不用添加题项");
          return;
        }
        this.$refs.addItemForm.addItem(record);
      },
      handleModalFormOk(topicId){
        console.log('MftTopicList', topicId);
        this.loading = true
        let param = {'topicId': topicId};
        getAction(this.url.itemList, param).then(res => {
          if (res.code == 200) {
            console.log(res.result.records);
            this.loading = false;
            this.innerData = res.result.records;
          }
        })
      },
      handleEditInner(record){
        this.$refs.addItemForm.edit(record);
      },
      handleDeleteInner(record){
        var that = this;
        deleteAction(that.url.itemDelete, {id: record.id}).then((res) => {
          if (res.success) {
            this.loading = true
            let param = {'topicId': record.topicId};
            getAction(this.url.itemList, param).then(res => {
              if (res.code == 200) {
                console.log(res.result.records);
                this.loading = false;
                this.innerData = res.result.records;
              }
            })
          } else {
            that.$message.warning(res.message);
          }
        });
      },
      initDictConfig(){
        loadCategoryData({code:'A01'}).then((res) => {
          if (res.success) {
            this.$set(this.dictOptions, 'specialType', res.result)
          }
        })
        loadCategoryData({code:'B02'}).then((res) => {
          if (res.success) {
            this.$set(this.dictOptions, 'topicCategory', res.result)
          }
        })
      },
      handleExpand(expanded, record) {
        console.log('expanded--' + expanded)
        this.expandedRowKeys = []
        this.innerData = []
        this.currentRecord = {}
        if (expanded === true) {
          this.loading = true
          this.expandedRowKeys.push(record.id)
          this.currentRecord = record
          console.log("点击行" + record.id);
          let param = {'topicId': record.id};
          getAction(this.url.itemList, param).then(res => {
            if (res.code == 200) {
              console.log(res.result.records);
              this.loading = false;
              this.innerData = res.result.records;
            }
          })
        }
      },
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'string',value:'topicTitle',text:'题目题干',dictCode:''})
        fieldList.push({type:'int',value:'specialType',text:'专项类别'})
        fieldList.push({type:'string',value:'topicType',text:'题型',dictCode:'topic_type'})
        fieldList.push({type:'string',value:'answer',text:'答案解析',dictCode:''})
        fieldList.push({type:'double',value:'score',text:'分数',dictCode:''})
        fieldList.push({type:'string',value:'must',text:'是否必练题',dictCode:''})
        fieldList.push({type:'int',value:'status',text:'状态',dictCode:'topic_status'})
        fieldList.push({type:'string',value:'topicPicture',text:'题目配图',dictCode:''})
        fieldList.push({type:'int',value:'isPay',text:'是否付费',dictCode:'topic_pay'})
        fieldList.push({type:'int',value:'isRight',text:'对错',dictCode:'topic_right'})
        fieldList.push({type:'string',value:'topicCategory',text:'题目类别'})
        fieldList.push({type:'string',value:'remark',text:'备注',dictCode:''})
        this.superFieldList = fieldList
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>